<!DOCTYPE html>
<meta charset="UTF-8">
<script type="text/html" id="toolbarRole">
	<div class="layui-btn-group ">
   		<button class="layui-btn layui-icon layui-icon-add-1" lay-event="add">新增</button>
  	  	<button class="layui-btn layui-icon layui-icon-edit" lay-event="update">修改</button>
  	  	<button class="layui-btn layui-icon layui-icon-delete" lay-event="delete">删除</button>
	</div>
</script>
<table id="roleTable" class="layui-table" lay-filter="roleTable"></table>
<script type="text/javascript">
	layui.use(['treetable', 'table' ,'layer'], function () {
		var table = layui.table;
		var layer = layui.layer;
		var treetable = layui.treetable;
		var crow = null;
		var tableData = null;
		var renderTable=function(){
			layer.load(2);
			treetable.render({
				//height:'full-160',
				id:'roleTable',
				treeColIndex: 1,
				treeSpid: '0',
				treeIdName: 'id',
				treePidName: 'parentId',
				treeDefaultClose: false,
				treeLinkage: false,
				elem: '#roleTable',
				url:'role/treeRoleList',
				toolbar:'#toolbarRole',
				page:false,
				cols:[
					[
						{type:'checkbox'},
						{field: 'name', title: '名称'},
	                    {field: 'code', title: '代码'},
	                    {field: 'idx1', title: '排序'}
					]
				],
				done: function () {
                    layer.closeAll('loading');
                    
                }
			});
		};
		renderTable();
		table.on('toolbar',function(obj){
        	switch(obj.event){
	        	case 'add':
	        		add();
	        		break;
	        	case 'update':
	        		update();
	        		break;
	        	case 'delete':
	        		del();
	        		break;
        	}
        });
		function add(){
			var checkStatus = table.checkStatus('roleTable');
			var data = checkStatus.data;
			var parentId = data.length==1?data[0].id:'';
			if(data.length >1){
				layer.msg("请选择一个父节点进行新增菜单！")
			}else{
				$.get('role/edit',{parentId:parentId},function(html){
					layer.open({
				    	type:1,
				    	title:'新增',
				    	area: ['500px','500px'],
				    	content:html,
				    	btn:['提交','退出'],
				    	success:function(layero,index){
				    		layui.use('form',function(){
				    			var form=layui.form;
				    			layero.addClass('layui-form');
				    			var submitBtn=layero.find('.layui-layer-btn0');
				    			submitBtn.attr('lay-filter','formVerify').attr('lay-submit','');
				    			layero.keydown(function(e){
				    				 if(e.keyCode==13){
				    					 submitBtn.click();
				    				 }
				    			 });
				    			form.on('submit(formVerify)',function(data){
				    				$.post('role/save',data.field,function(result){
				    					if(result.success){
				    						layer.close(index);
				    						renderTable();
				    					}
				    					layer.msg(result.msg);
				    				});
				    				return false;
				    			});
				    		});
				    	}
					});
				});
			}
		}
		function update(){
			var checkStatus = table.checkStatus('roleTable');
			var data = checkStatus.data;
			var parentId = data.length==1?data[0].id: '';
			if(data.length != 1){
				layer.alert('请先选择一条数据再进行修改操作！');
				return;
			}
			$.get('role/edit',{id:parentId},function(html){
				layer.open({
					id:'update',
			    	type:1,
			    	title:'修改',
			    	area: ['500px','500px'],
			    	content:html,
			    	btn:['提交','退出'],
			    	success:function(layero,index){
			    		layui.use('form',function(){
			    			var form=layui.form;
			    			layero.addClass('layui-form');
			    			var submitBtn=layero.find('.layui-layer-btn0');
			    			submitBtn.attr('lay-filter','formVerify').attr('lay-submit','');
			    			layero.keydown(function(e){
			    				 if(e.keyCode==13){
			    					 submitBtn.click();
			    				 }
			    			 });
			    			form.on('submit(formVerify)',function(data){
			    				$.post('role/save',data.field,function(result){
			    					if(result.success){
			    						layer.close(index);
			    						renderTable();
			    					}
			    					layer.msg(result.msg);
			    				});
			    				return false;
			    			});
			    		})
			    	}
				});
			})
		}
		function del(){
			var checkStatus = table.checkStatus('roleTable');
			var data = checkStatus.data;
			var parentId = data.length==1?data[0].id: '';
			if(data.length != 1){
				layer.alert('请先选择一条数据再进行删除！');
				return;
			}else{
				var id = data[0].id;
				layer.confirm('真的删除选定行的数据吗?', function(index){
					$.post('role/delete',{id:id},function(result){
						if(result.success){
							layer.close(index);
							renderTable();
						}
						layer.msg(result.msg);
					});
				});
			}
		}
	});
</script>
